package com.learn;

import java.util.Arrays;

public class quickSort {
    public static void quickSort(int[] arry, int begin, int end) {
        if (arry.length < 2 || begin >= end) {
            return;
        }
        int pivotIndex = parition(arry, begin, end);
        quickSort(arry, begin, pivotIndex - 1);
        quickSort(arry, pivotIndex + 1, end);
    }

    public static int parition(int[] arry, int begin, int end) {
        int pivot = arry[end];
        int pivotIndex = begin;
        for (int i = begin; i < end; i++) {
            if (arry[i] < pivot) {
                if (i > pivotIndex) {
                    swap(arry, i, pivotIndex);
                }
                pivotIndex++;
            }
        }
        swap(arry, pivotIndex, end);
        return pivotIndex;
    }

    public static void swap(int[] arry, int i, int j) {
        int temp = arry[j];
        arry[j] = arry[i];
        arry[i] = temp;
    }

    public static void main(String[] args) {
        int[] arry = {2, 1, 5, 3, 0, 7, 8, 4};
        quickSort(arry, 0, 7);
        System.out.println(Arrays.toString(arry));
    }
}
